package com.woniu.controller.medicalService;

import com.woniu.model.medicalService.entity.Department;
import com.woniu.model.gh.common.Result;
import com.woniu.model.gh.utils.MapUtil;
import com.woniu.model.gh.utils.PageVo;
import com.woniu.model.medicalService.vo.DepartmentVo;
import com.woniu.service.medicalService.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//科室表
@RestController
@RequestMapping("/ms_department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //查询所有一级科室
    @GetMapping("/findFirstLevels")
    public Result<List<Department>> findFirstLevels() {
        List<Department> list = departmentService.getFirstLevels();
        return Result.ok(list);
    }

    //查询所有二级科室
    @GetMapping("/findSecondLevels")
    public Result<List<Department>> findSecondLevels() {
        List<Department> list = departmentService.getSecondLevels();
        return Result.ok(list);
    }

    //分页 条件查询(根据一级科室id和二级科室名称模糊查询)
    @PostMapping("/findPageByKeyword")
    public Result<PageVo<DepartmentVo>> findPageByKeyword(@RequestBody Map<String,Object> condition) {

        Long firstLevelId = MapUtil.getLong("firstLevelId",null,condition);
        Integer pageNum = MapUtil.getInteger("pageNum", 1, condition);
        Integer pageSize = MapUtil.getInteger("pageSize", 5, condition);
        String secondLevelName = MapUtil.getString("secondLevelName", condition);

        PageVo<DepartmentVo> page = departmentService.findPageByKeyword(firstLevelId,secondLevelName,pageNum,pageSize);
        return Result.ok(page);
    }

    //添加二级科室
    @PostMapping("/save")
    public Result<Void> saveIIDepartment(@RequestBody Department department){
        departmentService.save(department);
        return Result.ok();
    }

    //按id删除二级科室
    @DeleteMapping
    public Result<Void> deleteById(Long id){
        departmentService.removeById(id);
        return Result.ok();
    }

    //按id修改
    @PostMapping("/update")
    public Result<Void> updateSecondLevel(@RequestBody Department department){
        departmentService.save(department);
        return Result.ok();
    }

}
